import { Message } from 'element-ui';
export function getCurrentDate() {
    let date = new Date();
    let yy = date.getFullYear();
    let mm = date.getMonth() + 1 > 10 ? date.getMonth() : '0' + (date.getMonth() + 1);
    let dd = date.getDate() > 10 ? date.getDate() : '0' + date.getDate();;
    return yy + "-" + mm + "-" + dd
}

export const $iscode = (res) => {

    if (res.errno === 0 || res.code == 0) {
        //  Message.success(res.errmsg);
        return true;
    } else if (res.code == 401) {


    } else {
        //console.log(res)
        Message.error(res.errmsg || '服务器维护中。。。');
        return false;
    }
}
export const $CircleShow = (map, center) => {
    var center = center;
    var map = map;


    let requestAnimationFrame =
        window.requestAnimationFrame || window.webkitRequestAnimationFrame;

    let cancelAnimationFrame =
        window.cancelAnimationFrame || window.webkitCancelAnimationFrame;

    function CircleShow(radius, level, color) {

        this.radius = radius;

        this.level = new Number(level);

        this.color = color;

        if (Number.isNaN(this.level)) {
            this.level = 1;
        } //至少1层

        if (!this.color || !this.color.fillColor) {
            this.color = {
                fillColor: "blue", //默认蓝色

                fillOpacity: 0.5, //默认初始透明度0.5
            };
        }

        //计算平均每段扩展距离的透明度

        this.endOpacity = 0.1; //终止透明度设置为0.1
        this.speedOpacity =
            (this.color.fillOpacity - this.endOpacity) / this.radius; //每米的透明度
        //先加一层白色的覆盖物，加在图片上表示覆盖范围

        //按层数循环构造覆盖物，并加在图片上

        this.circles = new Array();

        for (let i = 1; i < this.level; i++) {

            let circle = new AMap.Circle({
                map: map,

                center: center,

                radius: 0,

                fillColor: this.color.fillColor,

                fillOpacity: this.color.fillOpacity,

                strokeWeight: 1,

                strokeColor: this.color.fillColor,

                strokeOpacity: this.color.fillOpacity,
                zIndex: 9999999,
            });

            this.circles.push(circle);
        }

        this.clock = new Array(this.level);
    }

    CircleShow.prototype.start = function(distance, t0) {

        let _self = this;

        /**
         * 定义动画函数
         * @param startTime 启动的初始时间
         * @param circle 圆形覆盖物对象
         * @param index 序号
         */

        function animateStart(startTime, circle, index) {
            //计算时间差
            let time = new Date().getTime() - startTime;
            if (time < 0) {
                circle.setRadius(0); //半径
                // circle.setFillOpacity(_self.color.fillColor);   //透明度
                // circle.setStrokeOpacity(_self.color.fillOpacity);   //透明度
                //如果未达到执行实现则直接等待
                _self.clock[index] = window.requestAnimationFrame(
                    animateStart.bind(null, startTime, circle, index)
                );
                return;
            }
            //计算当前半径
            //匀减速运动下，每隔t时间，应该扩散的半径:
            //r=r0*(2*t*t0-t*t)/t0
            //其中，r0为最终的扩散半径，即this.radius
            let r = Math.floor(
                _self.radius * ((2 * time) / t0 - (time * time) / t0 / t0)
            );
            let opacity = 0;
            if (time >= t0) {
                //达到运行时间之后
                //设置圆形覆盖物的样式
                circle.setRadius(_self.radius); //半径
                // circle.setFillOpacity(_self.endOpacity);    //透明度
                // circle.setStrokeOpacity(_self.endOpacity);  //透明度

                startTime = new Date().getTime() + distance; //起始时间设置为当前时间加上1倍的时间间隔
                _self.clock[index] = window.requestAnimationFrame(
                    animateStart.bind(null, startTime, circle, index)
                );
            } else {
                //计算透明度
                let opacity =
                    _self.color.fillOpacity -
                    Number.parseFloat((_self.speedOpacity * r).toFixed(5)); //四舍五入小数点后5位

                //设置圆形覆盖物的样式
                circle.setRadius(r); //半径
                // circle.setFillOpacity(opacity);    //透明度
                // circle.setStrokeOpacity(opacity);   //透明度

                _self.clock[index] = window.requestAnimationFrame(
                    animateStart.bind(null, startTime, circle, index)
                );
            }
        }

        //循环每一层执行动画

        for (let [index, circle] of this.circles.entries()) {
            animateStart(new Date().getTime() + index * distance, circle, index);
        }
    };


    return CircleShow
}

export const $CircleLei = (map, center, m) => {
    var object3Dlayer = new AMap.Object3DLayer();
    map.add(object3Dlayer);
    var radar;
    var buildRadar = function() {
        radar = new AMap.Object3D.Mesh();
        radar.transparent = true;
        radar.backOrFront = 'front';

        var geometry = radar.geometry;
        var radius = m; // 米
        radius = radius / map.getResolution(map.getCenter(), 20);
        var unit = 1;
        var range = 200;
        var count = range / unit;

        for (var i = 0; i < count; i += 1) {
            var angle1 = i * unit * Math.PI / 180;
            var angle2 = (i + 0.5) * unit * Math.PI / 180;

            var p1x = Math.cos(angle1) * radius;
            var p1y = Math.sin(angle1) * radius;
            var p2x = Math.cos(angle2) * radius;
            var p2y = Math.sin(angle2) * radius;

            geometry.vertices.push(0, 0, 0);
            geometry.vertices.push(p1x, p1y, 0);
            geometry.vertices.push(p2x, p2y, 0);

            var opacityStart = getOpacity(i / count);
            var opacityEnd = getOpacity((i + 1) / count);

            geometry.vertexColors.push(1.25, 0.83, 2.55, opacityStart);
            // geometry.vertexColors.push(0, 1, 0.2, opacityStart);
            geometry.vertexColors.push(1.25, 0.83, 2.55, opacityEnd);
        }

        radar.position(center);

        object3Dlayer.add(radar);
    };

    function getOpacity(scale) {
        return 1 - Math.pow(scale, 1);
    }

    function scan() {
        radar.rotateZ(-2);

    }

    function start() {
        buildRadar();
        scan();
    }

    function end() {
        object3Dlayer.remove(radar);
        if (window.tt) {
            clearInterval(window.tt);
        }
    }
    return {
        object3Dlayer: object3Dlayer,
        start: start,
        scan: scan,
        end: end,

    }
}